# grid=[[1,3,1],[1,5,1],[4,2,1]]
grid = [[1,2,3],[4,5,6]]
def minPathSum(grid):
    grid_row=len(grid)
    grid_col=len(grid[0])
    dp=[[1 for _ in range(grid_col)] for _ in range(grid_row)]

    for i in range(grid_row):
        for j in range(grid_col):
            if i==0:
                if j>0:
                    dp[i][j]=dp[i][j-1]+grid[i][j]
                if j==0:
                    dp[i][j]=grid[i][j]
            if j==0:
                if i>0:
                    dp[i][j]=dp[i-1][j]+grid[i][j]
            if i>0 and i<grid_row and j>0 and j<grid_col:
                dp[i][j]=min(dp[i-1][j],dp[i][j-1])+grid[i][j]
    return dp[grid_row-1][grid_col-1]

print(minPathSum(grid))